package com.qf.controller;

import com.qf.common.ResponseResult;
import com.qf.entity.Banner;
import com.qf.request.RequestParams;
import com.qf.service.BannerService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/*
   @author 86155
   @ClassName: com.qf.controller.BannerController
   @Description: 该类的功能描述
   @date: 2021/9/29 18:59 
*/
@RestController
@RequestMapping("/banner")
public class BannerController {
    @Resource
    private BannerService bannerService;

    @PostMapping("/add")
    public ResponseResult<Integer> addBanner(@RequestBody RequestParams requestParams) {
        try {
            int i = bannerService.addBanner(requestParams);
            if(i>0){
                return ResponseResult.success(i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.error();
    }

    @PutMapping("/update")
    public ResponseResult<Integer> updateBanner(@RequestBody RequestParams requestParams){
        try {
            Integer i= bannerService.updateBanner(requestParams);
            if(i>0){
                return ResponseResult.success(i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.error();
    }

    @DeleteMapping("/delete/{bid}")
    public ResponseResult<Integer> deleteBanner(@PathVariable("bid") Long bid){
        try {
            Integer i=  bannerService.deleteBanner(bid);
            if(i>0){
                return ResponseResult.success(i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.error();
    }

    @GetMapping("/find")
    public ResponseResult<List<Banner>> findAllBanner(String bannerName,
                                                      @RequestParam(defaultValue = "1") Integer page,
                                                      @RequestParam(defaultValue = "10") Integer size){
        try {
            List<Banner>banners = bannerService.selectAllBanner(bannerName,page,size);
            return ResponseResult.success(banners);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.error();
    }

}
